package cn.cdtu.tibetplan.controller;
import java.util.List;

import cn.cdtu.tibetplan.pojo.*;
import cn.cdtu.tibetplan.service.SchoolService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.cdtu.tibetplan.service.EnrollService;

/**
 * controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/enroll")
public class EnrollController {

	@Autowired
	private EnrollService enrollService;
	@Autowired
	private SchoolService schoolService;
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<Enroll> findAll(){
		return enrollService.findAll();
	}

	@RequestMapping("/list")
	public String list() {
		return "redirect:search?currentPage=1&pageSize=10";
	}

	@RequestMapping("/search")
	public String search(Enroll enroll, int currentPage, int pageSize, Model model) {
		List<School> schools = schoolService.findAll();
		model.addAttribute("schools", schools);
		PageResult result = enrollService.findPage(enroll, currentPage, pageSize);
		PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
		model.addAttribute("pageEnd", pageend);
		return "/Enroll/index";
	}
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(int page, int rows){
		return enrollService.findPage(page, rows);
	}
	
	/**
	 * 增加
	 * @param enroll
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add")
	public Result add(Enroll enroll){
		try {
			enrollService.add(enroll);
			return new Result(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}
	
	/**
	 * 修改
	 * @param enroll
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody Enroll enroll){
		try {
			enrollService.update(enroll);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public Enroll findOne(Long id){
		return enrollService.findOne(id);		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(Long [] ids){
		try {
			enrollService.delete(ids);
			return new Result(true, "删除成功"); 
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
}
